﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xinkui.Common;
using System.Web;
using Xinkui.Common.Enum;

namespace Xinkui.WebModel.Helper {
    public class UIHelper {

        public static string BindImage(object url, ImageSizeEnum size = ImageSizeEnum.S100) {
            string imageLoc = string.Empty;
            if (url == null) {
                imageLoc = GetEmptyImageLocation(size);
            }
            else {
                imageLoc = url.ToString();
            }

            return URLHelper.Resource(imageLoc);
        }

        public static string BindingImage(object filename, ESourceType sourceType, ImageSizeEnum size = ImageSizeEnum.S100) {
            string imageLoc = string.Empty;
            if (filename == null || filename.ToString().Length <= 3) {
                imageLoc = GetEmptyImageLocation(size);
            }
            else {
                imageLoc = FileUpload.GetDirectoryPath(filename.ToString(), sourceType);

                int s = GetSize(size);
                imageLoc = imageLoc.Replace(".", String.Format("{0}_{0}.", s));
            }

            return URLHelper.Resource(imageLoc.TrimStart('/'));
        }

        public static string GetEmptyImageLocation(ImageSizeEnum size) {
            switch (size) {
                case ImageSizeEnum.S60: return SystemConst.UIConst.EmptyImageFor60;
                case ImageSizeEnum.S80: return SystemConst.UIConst.EmptyImageFor80;
                case ImageSizeEnum.S100: return SystemConst.UIConst.EmptyImageFor100;
                case ImageSizeEnum.S200: return SystemConst.UIConst.EmptyImageFor200;
                case ImageSizeEnum.S400: return SystemConst.UIConst.EmptyImageFor400;
                case ImageSizeEnum.SDef:
                default:
                    return SystemConst.UIConst.EmptyImageForDef;
            }
        }

        public static int GetSize(ImageSizeEnum size) {
            switch (size) {
                case ImageSizeEnum.S60: return 60;
                case ImageSizeEnum.S80: return 80;
                case ImageSizeEnum.S100: return 100;
                case ImageSizeEnum.S200: return 200;
                case ImageSizeEnum.S400: return 400;
                case ImageSizeEnum.SDef:
                default:
                    return 0;
            }
        }

        public static string FormatDate(object date) {
            if (date == null) return string.Empty;
            if (date is DateTime) {
                var d = (DateTime)date;
                return d.ToString("yyyy-MM-dd");
            }
            else {
                return string.Empty;
            }
        }

        public static string Truncate(object o, int max) {
            if (o == null) return string.Empty;
            return CommonFramework.Utility.StringHelper.CutString(o.ToString(), max, "...");
        }

        public static string TruncateAndNoHTML(object o, int max) {
            if (o == null) return string.Empty;
            return Truncate(CommonFramework.Utility.StringHelper.NoHTML(o.ToString()), max);
        }

        public static void ShowSuucessTips(string msg) {
            ShowTips(msg, 1, string.Empty);
        }

        public static void ShowSuucessTips(string msg, string url) {
            ShowTips(msg, 1, url);
        }

        public static void ShowWaringTips(string msg) {
            ShowTips(msg, 0, string.Empty);
        }

        public static void ShowErrorTips(string msg) {
            ShowTips(msg, 3, string.Empty);
        }


        public static void ShowTips(string msg, int type/*0-success 1 waring 2 error*/, string url) {
            var handler = (HttpContext.Current.Handler as System.Web.UI.Page);
            handler.ClientScript.RegisterStartupScript(handler.GetType(), string.Format("SHOW_MSG_{0}", type), string.Format("showmsg({0},'{1}','{2}');", type, msg, url), true);
        }

    }
}
